# Contributor: Robert T. McGibbon <rmcgibbo@gmail.com>

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.9.2
pkgrel=1
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:BSD-3-Clause')
url="https://www.unidata.ucar.edu/software/netcdf/"
depends=("${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
checkdepends=("unzip")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/Unidata/netcdf-c/archive/v${pkgver}.tar.gz"
        "0001-no-debug-libraries.patch")
sha256sums=('bc104d101278c68b303359b3dc4192f81592ae8640f1aee486921138f7f88cb7'
            'a16300682b43de63bead0a42211b2219820599a6eb3d60bc83c27f85a1c64711')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-c-${pkgver}"

  apply_patch_with_msg \
    0001-no-debug-libraries.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # disable multitude of warnings
  CFLAGS+=" -Wno-sign-conversion -Wno-float-conversion"

  msg2 "Building static libraries"
  mkdir -p "${srcdir}"/build-static-${MSYSTEM} && cd "${srcdir}"/build-static-${MSYSTEM}

  # Disable BYTERANGE because that feature doesn't build with HDF5 1.14 in netCDF 4.9.1
  # It was disabled by default in netCDF 4.9.0
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_DLL=OFF \
    -DENABLE_TESTS=OFF \
    -DENABLE_DAP_REMOTE_TESTS=OFF \
    -DENABLE_DAP=ON \
    -DENABLE_NETCDF_4=ON \
    -DENABLE_BYTERANGE=OFF \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  msg2 "Building shared libraries"
  mkdir -p "${srcdir}"/build-shared-${MSYSTEM} && cd "${srcdir}"/build-shared-${MSYSTEM}

  # Disable BYTERANGE because that feature doesn't build with HDF5 1.14 in netCDF 4.9.1
  # It was disabled by default in netCDF 4.9.0
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_EXAMPLES=OFF \
    -DENABLE_TESTS=OFF \
    -DENABLE_DAP_REMOTE_TESTS=OFF \
    -DENABLE_DAP=ON \
    -DENABLE_NETCDF_4=ON \
    -DENABLE_BYTERANGE=OFF \
    -DENABLE_LOGGING=ON \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake.exe -DENABLE_TESTS=ON ../${_realname}-c-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .
  "${MINGW_PREFIX}"/bin/ctest
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-static-${MSYSTEM}

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-shared-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_realname}-c-${pkgver}/COPYRIGHT \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/COPYRIGHT

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/netCDF/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  local SRCDIR_WIN=$(cygpath -wm "${srcdir}")
  for file in \
    "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc \
    "${pkgdir}${MINGW_PREFIX}/bin/nc-config" \
    "${pkgdir}${MINGW_PREFIX}/lib/libnetcdf.settings" \
    "${pkgdir}${MINGW_PREFIX}/lib/cmake/netCDF/netCDFConfig.cmake" \
    ; do
    sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
      -e "s|${SRCDIR_WIN}|\$\{_SRCDIR\}|g" "$file"
  done
}
